package guoanmaker.operator.business.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import guoanmaker.operator.business.modelnobind.OperatorContractBind;
import guoanmaker.operator.business.modelnobind.OperatorContractTypeBind;


/**
 * 
* @ClassName: OperatorContractDao 
* @Description: TODO(合同绑定数据操作类) 
* @author (ZhangXinYu)  
* @date 2017年12月17日 下午1:52:30 
* @version V1.0
 */
@Repository
public class OperatorContractDao {
	
	@Autowired
	private EntityManager entityManager;
	
	/**
	 * 查询所有合同信息
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object>  contractTypes(Integer currentPage , Integer PageSize,String guide) {
		Map<String, Object>  map=new HashMap<>();
		if(guide==null||guide.equals("")){
			String jpql = "select new guoanmaker.operator.business.modelnobind.OperatorContractTypeBind(o.id, o.name, o.useCount, o.createDate, o.guide) from OperatorContractType o  where 1=1";
			jpql +=" and (o.guide !='1' or o.guide is NULL or o.guide='')";
			jpql += " order by o.createDate desc";
			if(currentPage==null){
				currentPage=0;
			}
			if (PageSize==null) {
				PageSize=10;
			}
			Query query = entityManager.createQuery(jpql);
			List<OperatorContractTypeBind> result = query.getResultList();
			map.put("totalElements", result.size());
			query.setFirstResult(currentPage*PageSize);
			query.setMaxResults(PageSize);
			List<OperatorContractTypeBind> result1 = query.getResultList();
			map.put("content", result1);
			return map;
		}else{
			String jpql = "select new guoanmaker.operator.business.modelnobind.OperatorContractTypeBind(c.id, c.name, c.useCount, c.createDate, c.guide) from OperatorContractType c  where 1=1";
			jpql +=" and c.guide ='1'";
			jpql += " order by c.createDate desc";
			if(currentPage==null){
				currentPage=0;
			}
			if (PageSize==null) {
				PageSize=10;
			}
			Query query = entityManager.createQuery(jpql);
			List<OperatorContractTypeBind> result = query.getResultList();
			map.put("totalElements", result.size());
			query.setFirstResult(currentPage*PageSize);
			query.setMaxResults(PageSize);
			List<OperatorContractTypeBind> result1 = query.getResultList();
			map.put("content", result1);
			return map;
		}
	}
	
	
	
	
	
	
	
	@SuppressWarnings("unchecked")
	public List<OperatorContractBind> findAll(String name , String typeid, Integer currentPage , Integer pageSize,String guide){
		List<OperatorContractBind> contracts = null;
		if(guide==null||guide.equals("")){
			String jpql = "select new guoanmaker.operator.business.modelnobind.OperatorContractBind(c.id,c.name,t.id,t.name,c.file,c.img,c.date) from OperatorContract c LEFT JOIN c.contractType t where 1=1";
			if(name != null && !name.equals("")){
				jpql += " and c.name like'%"+name+"%'";
			}
			if(typeid != null && !typeid.equals("")){
				jpql += " and t.id='"+typeid+"'";
			}
			jpql +=" and (c.guide !='1' or c.guide is NULL or c.guide='')";
			jpql += " order by c.date desc";
			Query query = entityManager.createQuery(jpql).setFirstResult(currentPage*pageSize).setMaxResults(pageSize);
			contracts = query.getResultList();
		}else{
			String jpql = "select new guoanmaker.operator.business.modelnobind.OperatorContractBind(c.id,c.name,t.id,t.name,c.file,c.img,c.date) from OperatorContract c LEFT JOIN c.contractType t where 1=1";
			if(name != null && !name.equals("")){
				jpql += " and c.name like'%"+name+"%'";
			}
			if(typeid != null && !typeid.equals("")){
				jpql += " and t.id='"+typeid+"'";
			}
			jpql += " and c.guide = '1'";
			jpql += " order by c.date desc";
			Query query = entityManager.createQuery(jpql).setFirstResult(currentPage*pageSize).setMaxResults(pageSize);
			contracts = query.getResultList();
		}
		return contracts;
	}
	
	@SuppressWarnings("unchecked")
	public List<OperatorContractBind> findByName(String name){
		List<OperatorContractBind> contracts = null;
		String jpql = "select new guoanmaker.operator.business.modelnobind.OperatorContractBind(c.id,c.name,t.id,t.name,c.file,c.img,c.date) from OperatorContract c LEFT JOIN c.contractType t where 1=1  and  (c.guide !='1' or c.guide is NULL or c.guide='') and c.name = '"+name+"'";
		jpql += " order by c.date desc";
		Query query = entityManager.createQuery(jpql);
		contracts = query.getResultList();
		return contracts;
	}
	@SuppressWarnings("unchecked")
	public List<OperatorContractBind> findByName1(String name){
		List<OperatorContractBind> contracts = null;
		String jpql = "select new guoanmaker.operator.business.modelnobind.OperatorContractBind(c.id,c.name,t.id,t.name,c.file,c.img,c.date) from OperatorContract c LEFT JOIN c.contractType t where 1=1  and c.guide ='1' and c.name = '"+name+"'";
		jpql += " order by c.date desc";
		Query query = entityManager.createQuery(jpql);
		contracts = query.getResultList();
		return contracts;
	}
	
	@SuppressWarnings("unchecked")
	public List<OperatorContractBind> findByNameAndIdNot(String name,String id){
		List<OperatorContractBind> contracts = null;
		String jpql = "select new guoanmaker.operator.business.modelnobind.OperatorContractBind(c.id,c.name,t.id,t.name,c.file,c.img,c.date) from OperatorContract c LEFT JOIN c.contractType t where 1=1 and c.name = '"+name+"' and c.id != '"+id+"'";
		Query query = entityManager.createQuery(jpql);
		contracts = query.getResultList();
		return contracts;
	}
	
	/**
	 * 合同条件查询数量
	 */
	@SuppressWarnings("unchecked")
	public Integer findAllCount(String name , String typeid,String guide){
		List<OperatorContractBind> contracts = null;
		if(guide==null||guide.equals("")){
			String jpql = "select new guoanmaker.operator.business.modelnobind.OperatorContractBind(c.id,c.name,t.id,t.name,c.file,c.img ,c.date) from OperatorContract c LEFT JOIN c.contractType t where 1=1";
			if(name != null && !name.equals("")){
				jpql += " and c.name like'%"+name+"%'";
			}
			if(typeid != null && !typeid.equals("")){
				jpql += " and t.id='"+typeid+"'";
			}
			jpql +=" and (c.guide !='1' or c.guide is NULL or c.guide='')";
			Query query = entityManager.createQuery(jpql);
			contracts = query.getResultList();
		}else{
			String jpql = "select new guoanmaker.operator.business.modelnobind.OperatorContractBind(c.id,c.name,t.id,t.name,c.file,c.img ,c.date) from OperatorContract c LEFT JOIN c.contractType t where 1=1";
			if(name != null && !name.equals("")){
				jpql += " and c.name like'%"+name+"%'";
			}
			if(typeid != null && !typeid.equals("")){
				jpql += " and t.id='"+typeid+"'";
			}
			jpql +=" and c.guide ='1'";
			Query query = entityManager.createQuery(jpql);
			contracts = query.getResultList();
		}
		return contracts.size();
	}

	
	/**
	 * 查询指定合同信息
	 */
	@SuppressWarnings("unchecked")
	public OperatorContractBind findContractInfo(String id){
		OperatorContractBind operatorContractBind = null;
		String jpql = "select new guoanmaker.operator.business.modelnobind.OperatorContractBind(c.id,c.name,t.id,t.name,c.file,c.img ,c.date) from OperatorContract c LEFT JOIN c.contractType t where 1=1 and c.id='"+id+"'";
		Query query = entityManager.createQuery(jpql);
		List<OperatorContractBind> contractBinds = query.getResultList();
		if(contractBinds != null && contractBinds.size() > 0){
			operatorContractBind = contractBinds.get(0);
		}
		return operatorContractBind;
	}
	
	/**
	 * 查询所有非本id信息
	 */
	@SuppressWarnings("unchecked")
	public List<OperatorContractBind> findAll(String id, String name , String typeid, Integer currentPage , Integer pageSize){
		List<OperatorContractBind> contracts = null;
		String jpql = "select new guoanmaker.operator.business.modelnobind.OperatorContractBind(c.id,c.name,t.id,t.name,c.file,c.img,c.date) from OperatorContract c LEFT JOIN c.contractType t where 1=1";
		jpql += " and c.id != '"+id+"'";
		if(name != null && !name.equals("")){
			jpql += " and c.name like'%"+name+"%'";
		}
		if(typeid != null && !typeid.equals("")){
			jpql += " and t.id='"+typeid+"'";
		}
		Query query = entityManager.createQuery(jpql).setFirstResult(currentPage*pageSize).setMaxResults(pageSize);
		contracts = query.getResultList();
		return contracts;
	}
}
